
import { Body, Controller, Get,Post,Query, Session } from '@nestjs/common';
import { UserService } from 'src/services/user/user.service'
import { IUser } from 'src/types/user.interface';
import HttpResponse from 'src/types/http.response.interface';

type UnionType = IUser[] | IUser;



@Controller('user')
export class UserController {
      //等价于上面的写法
      constructor(private readonly  userService:UserService){

      }
      /**
     * 用来通过postman或者insomnia来导入数据
     * 在浏览器中输入http://localhost:9898/user/generatecode
     */
    
    @Post('/generatecode')
    async generatecode( 
        @Body('phone') phone:string ,
        @Session() session:Record<string,any>
    ) {
        session.verificationCode = {phone};
        const data:UnionType = await this.userService.generateCode(phone);
        return {
            success:true,
            message:"数据导入成功",
            data
        };
    }

    /**
     * 
     * @param phone 接受前端传递过来的参数
     * @param code 接受前端传递过来的参数
     * @returns 
     */
    @Post('/checkCode')
    async checkCode( @Body('phone') phone:string,  @Body('code') code:string) {
        const data = await this.userService.checkCode(phone,code);
        if(Array.isArray(data) && data.length>0){
            return {
                success:true,
                message:`验证码是该${phone}号发送的`
            };
        }
        else{
            return {
                success:false,
                message:`${code}验证码无效，请重新获取`
            };
        }
        
    }

    /**
     * 使用验证码登录接口
     * @param phone 
     * @param code 
     * @returns 
     */
    @Post('/loginWithVerifyCode')
    async loginWithVerifyCode( @Body('phone') phone:string,  @Body('code') code:string) {
        const data = await this.userService.loginWithVerifyCode(phone,code);
        if(Array.isArray(data) && data.length>0){
            return {
                success:true,
                message:`登录成功`,
                data
            };
        }
        else{
            return {
                success:false,
                message:`登录失败，请检查验证码或者手机号是否输入正确`
            };
        }
        
    }


    @Post('/loginWithPassword')
    async loginWithPassword( @Body('phone') phone:string,  @Body('password') password:string) {
        const data = await this.userService.loginWithPassword(phone,password);
        if(Array.isArray(data) && data.length>0){
            return {
                success:true,
                message:`登录成功`,
                data
            };
        }
        else{
            return {
                success:false,
                message:`登录失败，用户名或者密码错误`
            };
        }
        
    }






    @Post("/register")
    async register(@Body('phone') phone:string,@Body('password') password:string) {
        const data = await this.userService.register(phone,password);
        console.log('注册-data==',data);
        if(Object.keys(data).length>0){
            return {
                success:true,
                data,
                message:`注册成功`
            };
        }
        else{
            return {
                success:false,
                message:`注册失败`
            };
        }

        
    }
  
}




